package com.warmlight.voicepacket.plugin;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.Surface;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import tv.danmaku.ijk.media.player.IjkTimedText;

/**
 * Created by weiweijinpeng on 2018/7/25.
 */

public class QmzbMediaIjkPlayer extends QmzbMediaInterface implements IMediaPlayer.OnPreparedListener, IMediaPlayer.OnVideoSizeChangedListener, IMediaPlayer.OnCompletionListener, IMediaPlayer.OnErrorListener, IMediaPlayer.OnInfoListener, IMediaPlayer.OnSeekCompleteListener,IMediaPlayer.OnBufferingUpdateListener, IMediaPlayer.OnTimedTextListener  {
    IjkMediaPlayer ijkMediaPlayer;

    @Override
    public void start() {
        try {
            ijkMediaPlayer.start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void prepare() {
        ijkMediaPlayer = new IjkMediaPlayer();
        /**
         * 实现首屏秒开的参数
         */
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 0);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 0);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "overlay-format", IjkMediaPlayer.SDL_FCC_RV32);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "http-detect-range-support", 0);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48);
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "max-buffer-size", 1024 * 1024);

        ijkMediaPlayer.setOnPreparedListener(QmzbMediaIjkPlayer.this);
        ijkMediaPlayer.setOnVideoSizeChangedListener(QmzbMediaIjkPlayer.this);
        ijkMediaPlayer.setOnCompletionListener(QmzbMediaIjkPlayer.this);
        ijkMediaPlayer.setOnErrorListener(QmzbMediaIjkPlayer.this);
        ijkMediaPlayer.setOnInfoListener(QmzbMediaIjkPlayer.this);
        ijkMediaPlayer.setOnBufferingUpdateListener(QmzbMediaIjkPlayer.this);
        ijkMediaPlayer.setOnTimedTextListener(QmzbMediaIjkPlayer.this);

        try {
            /**
             * 调用服务器，设置数据源
             */
            ijkMediaPlayer.setDataSource(currentDataSource.toString());
            ijkMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            ijkMediaPlayer.setScreenOnWhilePlaying(true);
            ijkMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pause() {
        try {
            ijkMediaPlayer.pause();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean isPlaying() {
        if (ijkMediaPlayer == null){
            return false;
        }
        return ijkMediaPlayer.isPlaying();
    }

    @Override
    public void release() {
        if (ijkMediaPlayer != null)
            ijkMediaPlayer.release();
    }

    @Override
    public long getCurrentPosition() {
        if (ijkMediaPlayer == null){
            return 0;
        }
        return ijkMediaPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return ijkMediaPlayer.getDuration();
    }

    @Override
    public void seekTo(long time) {
        if (ijkMediaPlayer != null) {
            ijkMediaPlayer.seekTo((int) time);
        }
    }

    @Override
    public void setSurface(Surface surface) {
        ijkMediaPlayer.setSurface(surface);
    }

    @Override
    public void setVolume(float leftVolume, float rightVolume) {
        ijkMediaPlayer.setVolume(leftVolume, rightVolume);
    }

    @Override
    public int getVideoWidth() {
        return ijkMediaPlayer.getVideoWidth();
    }

    @Override
    public int getVideoHeight() {
        return ijkMediaPlayer.getVideoHeight();
    }

    @Override
    public long getTcpSpeed() {
        return ijkMediaPlayer.getTcpSpeed();
    }

    @Override
    public float getVideoOutputFramesPerSecond() {
        return ijkMediaPlayer.getVideoOutputFramesPerSecond();
    }

    @Override
    public float getVideoDecodeFramesPerSecond() {
        return ijkMediaPlayer.getVideoDecodeFramesPerSecond();
    }

    @Override
    public long getBitRate() {
        return ijkMediaPlayer.getBitRate();
    }

    @Override
    public int getVideoDecoder() {
        return ijkMediaPlayer.getVideoDecoder();
    }

    @Override
    public long getVideoCachedBytes() {
        return ijkMediaPlayer.getVideoCachedBytes();
    }

    @Override
    public long getVideoCachedDuration() {
        return ijkMediaPlayer.getVideoCachedDuration();
    }

    @Override
    public long getAudioCachedDuration() {
        return ijkMediaPlayer.getAudioCachedDuration();
    }

    @Override
    public long getAudioCachedBytes() {
        return ijkMediaPlayer.getAudioCachedBytes();
    }

    @Override
    public void onPrepared(IMediaPlayer iMediaPlayer) {
        ijkMediaPlayer.start();
        if (currentDataSource.toString().toLowerCase().contains("mp3")) {
            QmzbMediaManager.instance().mainThreadHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (QmzbVideoPlayerManager.getCurrentJzvd() != null) {
                        QmzbVideoPlayerManager.getCurrentJzvd().onPrepared();
                    }
                }
            });
        }
    }

    @Override
    public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int i, int i1, int i2, int i3) {
        QmzbMediaManager.instance().currentVideoWidth = iMediaPlayer.getVideoWidth();
        QmzbMediaManager.instance().currentVideoHeight = iMediaPlayer.getVideoHeight();
        QmzbMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (QmzbVideoPlayerManager.getCurrentJzvd() != null) {
                    QmzbVideoPlayerManager.getCurrentJzvd().onVideoSizeChanged();
                }
            }
        });
    }

    @Override
    public void onCompletion(IMediaPlayer iMediaPlayer) {
        QmzbMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (QmzbVideoPlayerManager.getCurrentJzvd() != null) {
                    QmzbVideoPlayerManager.getCurrentJzvd().onAutoCompletion();
                }
            }
        });
    }

    @Override
    public boolean onError(IMediaPlayer iMediaPlayer, final int what, final int extra) {
        QmzbMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (QmzbVideoPlayerManager.getCurrentJzvd() != null) {
                    QmzbVideoPlayerManager.getCurrentJzvd().onError(what, extra);
                }
            }
        });
        return true;
    }

    @Override
    public boolean onInfo(IMediaPlayer iMediaPlayer, final int what, final int extra) {
        QmzbMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (QmzbVideoPlayerManager.getCurrentJzvd() != null) {
                    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                        QmzbVideoPlayerManager.getCurrentJzvd().onPrepared();
                    } else {
                        QmzbVideoPlayerManager.getCurrentJzvd().onInfo(what, extra);
                    }
                }
            }
        });
        return false;
    }

    @Override
    public void onBufferingUpdate(IMediaPlayer iMediaPlayer, final int percent) {
        QmzbMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (QmzbVideoPlayerManager.getCurrentJzvd() != null) {
                }
            }
        });
    }
    @Override
    public void onTimedText(IMediaPlayer iMediaPlayer, IjkTimedText ijkTimedText) {

    }

    @Override
    public void onSeekComplete(IMediaPlayer iMediaPlayer) {
        QmzbMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (QmzbVideoPlayerManager.getCurrentJzvd() != null) {
                    QmzbVideoPlayerManager.getCurrentJzvd().onSeekComplete();
                }
            }
        });
    }
}
